//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef _BSPUSB11_H_
#define _BSPUSB11_H_

//#define BYTE    unsigned char
//#define UCHAR   unsigned char
//#define UINT    unsigned int
//#define USHORT  unsigned short
//#define WORD    unsigned short

typedef struct
{
    unsigned char bmRequest, bRequest;
    unsigned short wValue, wIndex, wLength;
} SetupPKG, *PSetupPKG;

// Request Codes - decimal
#define STD_GET_STATUS       0
#define STD_CLEAR_FEATURE    1
#define STD_SET_FEATURE      3
#define STD_SET_ADDRESS      5
#define STD_GET_DESCRIPTOR   6
#define STD_SET_DESCRIPTOR   7
#define STD_GET_CONFIG       8
#define STD_SET_CONFIG       9
#define STD_GET_INTERFACE   10
#define STD_SET_INTERFACE   11
#define STD_SYNCH_FRAME  12

//-------------------------------------------------------------------------
// Standard Chapter 9 definition
//-------------------------------------------------------------------------

// Request Codes
#define GET_STATUS      0x00
#define CLEAR_FEATURE   0x01
#define SET_FEATURE     0x03
#define SET_ADDRESS     0x05
#define GET_DESCRIPTOR  0x06
#define SET_DESCRIPTOR  0x07
#define GET_CONFIG      0x08
#define SET_CONFIG      0x09
#define GET_INTERFACE   0x0A
#define SET_INTERFACE   0x0B
#define SYNCH_FRAME     0x0C
#define VENDOR_COMMAND  0x60

// Standard Feature Selectors
#define DEVICE_REMOTE_WAKEUP        0x1
#define ENDPOINT_HALT               0x0

/*
 * Device and/or Interface Class codes
 * as found in bDeviceClass or bInterfaceClass
 * and defined by www.usb.org documents
 */
#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
#define USB_CLASS_AUDIO         1
#define USB_CLASS_COMM          2
#define USB_CLASS_HID           3
#define USB_CLASS_PHYSICAL      5
#define USB_CLASS_STILL_IMAGE   6
#define USB_CLASS_PRINTER       7
#define USB_CLASS_MASS_STORAGE  8
#define USB_CLASS_HUB           9
#define USB_CLASS_CDC_DATA      0x0a
#define USB_CLASS_CSCID         0x0b /* chip+ smart card */
#define USB_CLASS_CONTENT_SEC   0x0d /* content security */
#define USB_CLASS_VIDEO         0x0e
#define USB_CLASS_APP_SPEC      0xfe
#define USB_CLASS_VENDOR_SPEC   0xff

/*
 * USB directions
 *
 * This bit flag is used in endpoint descriptors' bEndpointAddress field.
 * It's also one of three fields in control requests bRequestType.
 */
#define USB_DIR_OUT   0x00  /* to device */
#define USB_DIR_IN    0x80  /* to host */

/* USB types, the second of three bRequestType fields */
#define USB_TYPE_MASK      (0x03 << 5)
#define USB_TYPE_STANDARD  (0x00 << 5)
#define USB_TYPE_CLASS     (0x01 << 5)
#define USB_TYPE_VENDOR    (0x02 << 5)
#define USB_TYPE_RESERVED  (0x03 << 5)

/* Descriptor Types */
#define USB_DT_DEVICE     0x01
#define USB_DT_CONFIG     0x02
#define USB_DT_STRING     0x03
#define USB_DT_INTERFACE  0x04
#define USB_DT_ENDPOINT   0x05

/* descriptor size */
#define USB_DT_DEVICE_SIZE    18
#define USB_DT_CONFIG_SIZE    9
#define USB_DT_INTERFACE_SIZE 9
#define USB_DT_ENDPOINT_SIZE  7

/* Endpoints */
#define USB_ENDPOINT_XFER_CONTROL 0
#define USB_ENDPOINT_XFER_ISOC    1
#define USB_ENDPOINT_XFER_BULK    2
#define USB_ENDPOINT_XFER_INT     3

/* from config descriptor bmAttributes */
#define USB_CONFIG_ATT_ONE       (1 << 7) /* must be set */
#define USB_CONFIG_ATT_SELFPOWER (1 << 6) /* self powered */
#define USB_CONFIG_ATT_WAKEUP    (1 << 5) /* can wakeup */

#define STR_INDEX_MANUFACTURER      0x00
#define STR_INDEX_PRODUCT           0x01
#define STR_INDEX_SERIALNUMBER      0x02

#define WAIT_FOR_SETUP      1
#define DATA_STATE_XMIT     2
#define DATA_STATE_RCVR     3
#define WAIT_FOR_OUT_STATUS 4
#define WAIT_FOR_IN_STATUS  5

#if defined(_INTEL) || defined(_EVC)
#define BSP_PACKED
#pragma pack(push, 1)
#else
#define BSP_PACKED __attribute__ ((packed))
#endif

typedef struct BSP_PACKED __device_descriptor
{
    UCHAR   bLength;
    UCHAR   bDescriptorType;
    USHORT  bcdUSB;
    UCHAR   bDeviceClass;
    UCHAR   bDeviceSubClass;
    UCHAR   bDeviceProtocol;
    UCHAR   bMaxPackectSize0;
    USHORT  idVendor;
    USHORT  idProduct;
    USHORT  bcdDevice;
    UCHAR   iManufacturer;
    UCHAR   iProduct;
    UCHAR   iSerialNumber;
    UCHAR   bNumConfigurations;
} device_descriptor, *Pdevice_descriptor;

typedef struct BSP_PACKED __configuration_descriptor
{
    UCHAR   bLength;
    UCHAR   bDescriptorType;
    USHORT  wTotalLength;
    UCHAR   bNumInterfaces;
    UCHAR   bConfigurationValue;
    UCHAR   iConfiguration;
    UCHAR   bmAttributes;
    UCHAR   bMaxPower;
} configuration_descriptor, *Pconfiguration_descriptor;

typedef struct BSP_PACKED __interface_descriptor
{
    UCHAR   bLength;
    UCHAR   bDescriptorType;
    UCHAR   bInterfaceNumber;
    UCHAR   bAlternateSetting;
    UCHAR   bNumEndpoints;
    UCHAR   bInterfaceClass;
    UCHAR   bInterfaceSubClass;
    UCHAR   bInterfaceProtocol;
    UCHAR   iInterface;
} interface_descriptor, *Pinterface_descriptor;

typedef struct BSP_PACKED __endpoint_descriptor
{
    UCHAR bLength;            //0
    UCHAR bDescriptorType;    //1
    UCHAR bEndpointAddress;   //2
    UCHAR bmAtrributes;       //3
    USHORT wMaxPacketSize;     //4
    UCHAR bInterval;          //6

}endpoint_descriptor,*Pendpoint_descriptor;

typedef struct BSP_PACKED __string_descriptor
{
    UCHAR *pbString;
    UCHAR bLength;
} string_descriptor, *Pstring_descriptor;

typedef struct BSP_PACKED __descriptor
{
    string_descriptor        String;
    device_descriptor        Device;
    configuration_descriptor Config;
    UCHAR                   *ConfigData;
//    interface_descriptor     Interface;
//    endpoint_descriptor      Endpoint;
} descriptor, *Pdescriptor;
#if defined(_INTEL) || defined(_EVC)
#pragma pack(pop)
#endif

#endif
